// import {formatUrlHash} from "@/utils/util"
import useClipboard from "vue-clipboard3";

const { toClipboard } = useClipboard();
export const TOKEN_PREFIX = "iThings";

export async function copyText(text: any) {
  try {
    await toClipboard(text);
    return true;
  } catch (e) {
    return false;
  }
}

/**
 * 2023-12 duanyb
 * @param str 需要解析的字符串
 * @param keys  字符串开始解析的关键字
 * @returns
 * 解析地址字符串，返回
 */
export const formatUrlHash = (str: string, keys: string) => {
  // console.log("str==",str);
  if (!str || !keys) return;
  const start: number = str.indexOf(keys);
  if (start == -1) return;
  str = str.substring(keys.length + 1);
  const data = str.split("&");
  const obj: any = {};
  data.forEach((item: any) => {
    const keyIndex: number = item.indexOf("=");
    const key: string = item.substring(0, keyIndex);
    const val: string = item.substring(keyIndex + 1);
    obj[key] = val;
  });
  return {
    hash: str,
    arr: data,
    data: obj,
  };
};
/**
 * 2023-12 duanyb
 * @param url
 * @returns
 * 获取地址栏携带的上级地址及携带参数，主要应用用户因某些操作权限导致需要跳转登录页，登录成功后跳转回原页面，并且页面跳转时销毁原页面
 */
export const getUrlData = (url: string) => {
  if (!url) return;
  // "/user/user-login?back_url=/?id=1&type=2"
  const index: number = url.indexOf("?back_url=");
  if (index != -1) {
    return url.substring(index + 10);
  }
  return;
};
